package com.gitee.ykr.lightweight.rpc.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 自定义ioc容器
 * @author YKR
 * @Describe
 * @date 2021/8/23
 */
@Component
public class NettyRpcApplication implements ApplicationContextAware {
    //容器
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        NettyRpcApplication.context = applicationContext;

    }

    /**
     * 通过名字获取bean
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return context.getBean( name );
    }

    /**
     * 通过类型获取bean
     * @param clz
     * @return
     */
    public static Object getBean(Class clz) {

        return context.getBean( clz );

    }

    /**
     * 如果是多例，根据类型获取bean的集合
     * @param clz
     * @return
     */
    public static Map<String, Object> getBeansOfType(Class clz) {

        return context.getBeansOfType( clz );
    }

    /**
     * 是否包含指定bean
     * @param name
     * @return
     */
    public static boolean containsBean(String name) {
        return context.containsBean( name );
    }
}
